package com.todo.client.gui.window.main;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;
import com.todo.client.gui.dialog.action.ActionDialog;
import com.todo.client.gui.dialog.user.login.LoginDialog;
import com.todo.client.messages.GUISettings;
import com.todo.client.messages.ToDoAppMessages;

public class MainWindow extends LayoutContainer {
	public static final GUISettings SETTINGS = GWT.create(GUISettings.class);
	public static final ToDoAppMessages MESSAGES = GWT
			.create(ToDoAppMessages.class);

	
	public MainWindow() {
		super();
		RootPanel.get().add(getViewport());
	}

	private Viewport getViewport() {
		Viewport viewport = new Viewport();
		viewport.setLayout(new BorderLayout());
		
		viewport.add(getCenterContainer(), getConteinerData());		
		viewport.add(getLeftPanel(), getLeftData());
		return viewport;
	}

	private ContentPanel getLeftPanel() {
		ContentPanel left = new ContentPanel();
		return left;
	}

	private ContentPanel getCenterPanel() {
		ContentPanel center = new ContentPanel();
		center.setBorders(false);
		center.setLayout(new FitLayout());
		center.addButton(getAddButton());
		center.addButton(getSignOutButton());
		center.add(getTypePanel());
		return center;
	}
	
	private ContentPanel getBottomPanel(){
		ContentPanel bottom = new ContentPanel();
		bottom.setBorders(false);
		bottom.setLayout(new FitLayout());
		return bottom;
	}

	private TreePanel<ModelData> getNavigationTree() {
		TreePanel<ModelData> tree = null;
		return tree;
	}
	
	private TabPanel getTypePanel(){
		TabPanel panel = new TabPanel();
		panel.setMinTabWidth(Integer.parseInt(SETTINGS.minTabWidth()));
		panel.setResizeTabs(true);
		panel.setAnimScroll(true);  
	    panel.setTabScroll(true);  
	    panel.setCloseContextMenu(true);
	    
	    panel.add(getItem(MESSAGES.start()));
	    panel.add(getItem(MESSAGES.low()));
	    panel.add(getItem(MESSAGES.medium()));
	    panel.add(getItem(MESSAGES.high()));
	    
		return panel;
	}
	
	private TabItem getItem(String title){
		TabItem item = new TabItem();
		item.setText(title);
		item.setClosable(true);
		item.addStyleName("pad-text");
		return item;
	}
	
	private Button getAddButton(){
		Button addButton = new Button(MESSAGES.addAction());
		addButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				final ActionDialog dialog = new ActionDialog(MESSAGES.addTitle());
				dialog.getPersonal().addSelectionListener(new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						dialog.hide();						
					}
				});
				dialog.show();
			}
		});
		
		return addButton;
	}
	private Button getSignOutButton(){
		Button signout = new Button(MESSAGES.signout());
		signout.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				//TODO: forward to LoginPage
				RootPanel.get().remove(MainWindow.this);
				RootPanel.get().add(new LoginDialog());				
			}
		});
		
		return signout;
	}

	private BorderLayoutData getLeftData() {
		BorderLayoutData data = new BorderLayoutData(LayoutRegion.WEST,
				Integer.parseInt(SETTINGS.leftSize()),
				Integer.parseInt(SETTINGS.leftMin()), Integer.parseInt(SETTINGS
						.leftMax()));
		data.setMargins(new Margins(Integer.parseInt(SETTINGS.leftTop()),
				Integer.parseInt(SETTINGS.leftRight()), Integer
						.parseInt(SETTINGS.leftBottom()), Integer
						.parseInt(SETTINGS.leftLeft())));
		data.setCollapsible(true);
		data.setSplit(true);

		return data;
	}

	private BorderLayoutData getCenterData() {
		BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
		data.setMargins(new Margins(Integer.parseInt(SETTINGS.centerTop()),
				Integer.parseInt(SETTINGS.centerRight()), Integer
						.parseInt(SETTINGS.centerBottom()), Integer
						.parseInt(SETTINGS.centerLeft())));
		data.setSplit(true);
		return data;
	}	
	
	private LayoutContainer getCenterContainer(){
		LayoutContainer conteiner = new LayoutContainer();
		conteiner.setLayout(new BorderLayout());
		conteiner.add(getCenterPanel(), getCenterData());
		conteiner.add(getBottomPanel(), getBottomData());
		
		return conteiner;
	}
	
	private BorderLayoutData getBottomData(){
		BorderLayoutData data = new BorderLayoutData(LayoutRegion.SOUTH);
		data.setMargins(new Margins(Integer.parseInt(SETTINGS.bottomTop()),
				Integer.parseInt(SETTINGS.bottomRight()), Integer
				.parseInt(SETTINGS.bottomBottom()), Integer
				.parseInt(SETTINGS.bottomLeft())));
		data.setSize(Integer.parseInt(SETTINGS.bottomSize()));
		data.setSplit(true);
		return data;
	}
	
	private BorderLayoutData getConteinerData(){
		BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
		data.setMargins(new Margins(Integer.parseInt(SETTINGS.centerTop()),
				Integer.parseInt(SETTINGS.centerRight()), Integer
						.parseInt(SETTINGS.centerBottom()), Integer
						.parseInt(SETTINGS.centerLeft())));
		data.setSplit(true);
		return data;
	}
}
